package mmc.ui;

import static mmc.resource.Text12p.glText12p;
import static org.lwjgl.input.Keyboard.*;
import static org.lwjgl.opengl.GL11.glColor4f;

import java.util.LinkedList;
import mmc.Client;

import org.lwjgl.opengl.GL11;

public class Chat extends Window {
	private String actualText="";
	private Client client;
	private LinkedList<String> list = new LinkedList<String>();
	private LinkedList<String> sendHistory = new LinkedList<String>();
	private int maxChatSize = 10;
	private int historyItem = -1;
	private int maxLength = 31;
	private float alpha = 1;
	private long lastAction = System.nanoTime();
	private static final long CHAT_FADEOUT_OFFSET_TIME = 5*1000000000L;
	private static final long CHAT_FADEOUT_TIME =        5*1000000000L;
	
	public Chat(float pos_x, float pos_y, float size_x, float size_y, Client client) {
		super(pos_x, pos_y, size_x, size_y);
		this.client = client;
		this.name = "Chat";
		
		KeyBinding.addWindowShortcut(this.name, KEY_RETURN, this);
	}

	@Override
	public void render() {
		if(open){
			lastAction = System.nanoTime();
			alpha = 1;
		}else{
			if(alpha>0){
				if(lastAction+CHAT_FADEOUT_OFFSET_TIME<System.nanoTime()){
					alpha = 1+(lastAction+CHAT_FADEOUT_OFFSET_TIME-System.nanoTime())/(float)CHAT_FADEOUT_TIME;
					if(alpha<0)
						alpha=0;
				}
			}
		}
		
		if(alpha > 0){
			
			glColor4f(0.54f, 0.25f, 0.07f, alpha); 		
			GL11.glBegin(GL11.GL_QUADS);
			GL11.glVertex2f(x-0.001f, y-0.001f);
			GL11.glVertex2f(x-0.001f, y+height+0.001f);
			GL11.glVertex2f(x+width+0.001f, y+height+0.001f);
			GL11.glVertex2f(x+width+0.001f, y-0.001f);
			GL11.glEnd();
			
			glColor4f(1, 1, 1, alpha); 
			
			GL11.glBegin(GL11.GL_QUADS);
			GL11.glVertex2f(x, y);
			GL11.glVertex2f(x, y+height);
			GL11.glVertex2f(x+width, y+height);
			GL11.glVertex2f(x+width, y);
			GL11.glEnd();
			
			
			int i = 3;
			synchronized(list){
				for (String str : list) {
					 
					if(str.startsWith("/w"))
						glColor4f(0.8f, 0, 0, alpha);
					else
						if(str.startsWith("Server"))
							glColor4f(0.8f, 0.2f, 0, alpha);
						else
							glColor4f(0, 0, 0, alpha);
					
					
									
					glText12p(str, x, y+height-0.02f*(i));
					i++;
					if(i>10)
						break;
				}
			}
			
			if(open)
			{
				glColor4f(1, 0.8f, 1, alpha); 
				GL11.glBegin(GL11.GL_QUADS);
				GL11.glVertex2f(x, y+height-0.04f);
				GL11.glVertex2f(x, y+height);
				GL11.glVertex2f(x+width, y+height);
				GL11.glVertex2f(x+width, y+height-0.04f);
				GL11.glEnd();			
				
				glColor4f(0, 0, 0, alpha);
				String text = actualText;
				if(text.length() > maxLength)
					text = text.substring(text.length()-maxLength, text.length());
				glText12p(text, x, y+height-0.03f);
			}
			
			glColor4f(1,1,1,1);
		}
	}

	@Override
	public void forwardKey(int key, char ch)
	{
		switch(key)
		{
			case KEY_RETURN:
				if("".equals(actualText)){
					client.ui.closeWindow();
					break;
				}
				if(actualText.startsWith("/w "))
				{				
					if(actualText.split(" ").length > 2)
					{
						String target =  actualText.split(" ")[1];						
						client.getScene().player.whisper(actualText.substring(actualText.indexOf(target)+target.length()+1, actualText.length()), target);
					}
				}
				else
				{
					client.getScene().player.say(actualText);
				}
				sendHistory.add(0, actualText);
				client.ui.closeWindow();			
				break;
			case KEY_BACK:
				if(actualText.length() > 0)
					actualText = actualText.substring(0, actualText.length()-1);
				break;
			case KEY_UP:			
				if(sendHistory.isEmpty()) break;
				historyItem = (++historyItem)%sendHistory.size();
				actualText = sendHistory.get(historyItem);
				break;
			case KEY_DOWN:
				if(sendHistory.isEmpty() || historyItem==-1) break;				
				historyItem = (--historyItem)%sendHistory.size();
				if(historyItem==-1)
					actualText = "";
				else
					actualText = sendHistory.get(historyItem);
				break;
			default:
				if(ch!=0)
					actualText += ch; //Keyboard.getKeyName(key);
		}
	}	
	
	@Override
	public void closeWindow()
	{
		open = false;
		actualText = "";
		historyItem = -1;
	}
	
	public void receiveMessage(String msg)
	{		
		lastAction = System.nanoTime();
		alpha = 1;
		
		synchronized(list){			
			String help = msg;
			String help2 = "";
			while(help != "")
			{				
				help2 = help.substring(0, (help.length()>maxLength)?maxLength:help.length());
				list.add(0, help2);				
				
				if(help.length() < maxLength)
					help = "";
				
				if(help2.length() > 0 && help.length() > 0)
					help = help.substring(help2.length(), help.length());
			}
			
			if(list.size() > maxChatSize)
				list.remove(list.size()-1);
		}
	}
}
